package
{
	import com.flashphoner.api.Flash_API;
	import com.flashphoner.api.data.PhoneConfig;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.Application;

	[Bindable]
	public class Account
	{
		public var username:String;
		public var password:String;
		public var flashAPI:Flash_API;

		public var timer:Timer;
		public function Account()
		{
		}
		
		private function login():void{
			Application.application.logs += username+" - execute login\n";
			flashAPI.login(username,password);
		}
		private function call():void{
			do{
				var randInt:int = PhoneConfig.getRandomInt(0,Accounts.allUsernames.length-1);				
				var callee:String = String(Accounts.allUsernames.getItemAt(randInt));
			}while (!Accounts.getInstance().callMe && callee == username);
			
			Application.application.logs += username+" - execute call to "+callee+"\n";
			flashAPI.call(callee, username);
		}
		
		private function logoff():void{
			flashAPI.logoff();
		}
		
		private function answer():void{
			flashAPI.answer();
		}	
		
		private function hangup():void{
			flashAPI.hangup();
		}		
		
		public function startTest():void{
			if (timer!=null){
				timer.removeEventListener(TimerEvent.TIMER,timerHandler);
				timer.stop();
				timer = null;
			}
			timer = new Timer(PhoneConfig.getRandomInt(5,10)*1000,1);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);					
			timer.start();			
		}
		
		private function timerHandler(timeEvent:TimerEvent):void{
			var accounts:Accounts = Accounts.getInstance();
			if (flashAPI.state == State.out){
				login();
			} else if (flashAPI.state == State.free){
				var value:int = PhoneConfig.getRandomInt(1,100);
				if (value <= accounts.freeCall){
					call();
				}else if (value > accounts.freeCall && value <= accounts.freeCall+accounts.freeLogoff){
					logoff();
				}
			} else if (flashAPI.state == State.incom){
				if (accounts.countTalk/2 > accounts.maxCountCall){
					hangup();
				}else if (accounts.countTalk/2 < accounts.minCountCall){
					answer();
				}else{
					var value:int = PhoneConfig.getRandomInt(1,100);
					if (value <= accounts.incomAnswer){
						answer();
					}else if (value > accounts.incomAnswer && value <= accounts.incomAnswer+accounts.incomHangup){
						hangup();
					}else if (value > accounts.incomAnswer+accounts.incomHangup && value <= accounts.incomAnswer+accounts.incomHangup+accounts.incomLogoff){
						logoff();
					}
				}
			} else if (flashAPI.state == State.call){
				var value:int = PhoneConfig.getRandomInt(1,100);
				if (value <= accounts.callHangup){
					hangup();
				}else if (value > accounts.callHangup && value <= accounts.callHangup+accounts.callLogoff){
					logoff();
				}				
			} else if (flashAPI.state == State.talk){
				if (flashAPI.timeOfCall > accounts.minTimeCall){
					var value:int = PhoneConfig.getRandomInt(1,100);
					if (value <= accounts.talkHangup){
						hangup();
					}else if (value > accounts.talkHangup && value <= accounts.talkHangup + accounts.talkLogoff){
						logoff();
					}
				}
			}
			startTest();
		}
		
		public function stopTest(){
			timer.removeEventListener(TimerEvent.TIMER,timerHandler);
			timer.stop();
			timer = null;
			if (flashAPI.state != State.out){
				logoff();
			}
		}
	}
}